package c2.advanced.server.configuration;

import cp.solution.business.cloud.CloudMongoEntity;
import cp.solution.business.game.boot.AdvancedSrvBootstrap;
import cp.solution.business.game.conf.AdvancedConfigEntity;
import easy.java.util.StrTool;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

public class AdvPostProcessUtils {

    public static String hps(Map<String, Object> map, AdvancedSrvBootstrap<? extends AdvancedConfigEntity> bootstrap, String mongoDbNameRule) {
        CloudMongoEntity cm = bootstrap.getMongo();
        String dbName;
        if(StringUtils.isNotBlank(bootstrap.getConfig().getMongoDbName())) {
            dbName = bootstrap.getConfig().getMongoDbName();
        } else {
            dbName = mongoDbNameRule;
        }
        // mongodb://用户名:密码@服务器IP:端口/数据库名
        String mongoUri;
        if(StringUtils.isNotBlank(cm.getUrl())) {
            if(cm.getUrl().contains("${db}")) {
                mongoUri = cm.getUrl().replace("${db}", dbName);
            } else {
                String cmUrl = cm.getUrl().endsWith("/") ? cm.getUrl() : cm.getUrl() + "/";
                mongoUri = StrTool.format("{}{}?authSource=admin", cmUrl, dbName);
            }
        } else {
            mongoUri = StrTool.format("mongodb://{}:{}@{}:{}/{}?authSource=admin", cm.getUsername(), cm.getPassword(), cm.getHost(), cm.getPort(), dbName);
        }
        map.put("spring.data.mongodb.uri", mongoUri);
        return "最终采用的mongoUri=" + mongoUri;
    }
}
